from utils.sql_utils import SQLManager
from utils.time_utils import TimeUtils
from utils.id_utils import IdUtils
from flask import request, jsonify

class CollectionService:
    def check_is_collection(request):
        user_id = request.get_json().get('user_id')
        note_id = request.get_json().get('note_id')
        print("check_is_collection:", user_id,note_id)
        sql = """
            SELECT 
                COUNT(*) AS count
            FROM 
                rb_note_collections
            WHERE 
                user_id = %s
            AND
                note_id = %s
            AND
                is_deleted = 0 ;
        """
        values = (user_id, note_id)
        result = SQLManager().get_one(sql, values)
        return result
    
    def cancel_collection(request):
        user_id = request.get_json().get('user_id')
        note_id = request.get_json().get('note_id')
        sql = """
            UPDATE 
                rb_note_collections 
            SET 
                is_deleted = 1
            WHERE 
                user_id = %s 
            AND 
                note_id = %s;
        """
        values = (user_id, note_id)
        return SQLManager().insert(sql, values)
    
    def get_collection_count_by_note_id(note_id):
        sql = """
            SELECT 
                COUNT(*) AS count
            FROM 
                rb_note_collections
            WHERE 
                note_id = %s
            AND
                is_deleted = 0 ;
        """
        values = (note_id)
        result = SQLManager().get_one(sql, values)
        return result
